<template>
  <div class="update-birthday">
      <van-datetime-picker
        v-model="currentDate"
        type="date"
        title="选择年月日"
        :min-date="minDate"
        :max-date="maxDate"
        @confirm="onSubmit"
        @cancel="$emit('close')"
      />
  </div>
</template>
<script>
import { updateUserProfile } from "@/api/user"
import dayjs from "dayjs"
export default {
    name:"UpdateBirthday",
    props:{
        value:{
            type:[String,Number]
        }
    },
    data(){
        return{
            minDate: new Date(1950, 0, 1),
            maxDate: new Date(),
            currentDate: this.value ? new Date(this.value) : new Date(),  
        }
    },
    created(){},
    methods:{
        // 修改资料
        async onSubmit(){
            this.$toast.loading({
                message:"保存中...",
                forbidClick:true,
                duration:0
            })
            try {
                const currentDate = dayjs(this.currentDate).format("YYYY-MM-DD")
                const { data } = await updateUserProfile({
                    birthday:currentDate,
                })
                // 更新视图
                this.$emit('input',currentDate)
                // 关闭弹框
                this.$emit('close')
                // 修改成功
                 this.$toast.success('更新成功')
            } catch (error) {
                console.log(error);
                 this.$toast.fail('更新失败')
            }
        }
    },
    components:{}
}
</script>
<style scoped lang="less">
.field-wrap{
    margin:30px;
    /deep/ .van-field__control{
        padding: 14px;
    }
}
</style>